---
title: Structured Output
description: Define JSON schemas to ensure structured and validated responses from AI models.
---

Latitude allows you to enforce structured JSON output from AI models by defining a JSON schema directly in the prompt's configuration (frontmatter). This ensures responses are consistent, automatically validated, and easy to integrate into your applications.

## Specifying the Schema

Use the `schema` key within the prompt's configuration block (`---`). Define the expected JSON structure using standard [JSON Schema](https://json-schema.org/) syntax.

```yaml {4-21}
---
provider: openai
model: gpt-4o
schema:
  type: object
  properties:
    sentiment:
      type: string
      description: The sentiment of the input text.
      enum: [positive, negative, neutral]
    confidence:
      type: number
      description: The confidence score (0.0 to 1.0).
      minimum: 0
      maximum: 1
    explanation:
      type: string
      description: A brief explanation for the sentiment classification.
  required:
    - sentiment
    - confidence
---
<system>
Analyze the sentiment of the following text and provide your analysis in JSON format according to the defined schema.
</system>

<user>
{{ user_text }}
</user>
```

In this example:

- We expect a JSON object (`type: object`).
- It must have `properties`: `sentiment`, `confidence`, and `explanation`.
- `sentiment` must be one of the strings in the `enum` list.
- `confidence` must be a `number` between 0 and 1.
- `explanation` is an optional `string`.
- `sentiment` and `confidence` are mandatory (`required` list).

## How it Works

When a `schema` is defined:

1.  **Provider Integration**: Latitude communicates the schema to the AI provider (if the provider supports JSON mode or function calling with schemas, like recent OpenAI, Anthropic, and Google models).
2.  **Model Guidance**: The model is instructed to generate output strictly adhering to the provided schema.

## Benefits

- **Reliability**: Guarantees consistent data structures from the AI.
- **Ease of Use**: Simplifies parsing and using AI responses in downstream code.
- **Reduced Errors**: Catches formatting issues automatically.
- **Clear Intent**: Explicitly tells the model the desired output format.

## Chains and Agents

When working with chains and agents, there's some things to keep in mind:

### Chains
When using chains, the configuration added to the general configuration section in the prompt will be applied to all steps in the chain. This means that if you have a schema defined in the general configuration, it will be applied to all steps, which may not be the expected behavior. To avoid this, you can define the schema in the specific step configuration instead.

```{5-7,9-27}
---
provider: openai
model: gpt-4o
---
<step>
  This step does not have a schema defined.
</step>

<step schema={{
  {
    type: 'object',
    properties: {
      success: {
        type: 'boolean',
        description: 'Indicates if the operation was successful.',
      },
      explanation: {
        type: 'string',
        description: 'A brief explanation of the operation.',
      },
    },
    required: ['success', 'explanation'],
    additionalProperties: false,
  }
}}>
  This step has a schema defined.
</step>
```

### Agents
When creating agents, the only schema to have in mind is the schema of the Agent response, since any intermediate steps are used as Chain of Thought, reasoning steps, or tool calling. This is why, even if an Agent has a `schema` property defined in the configuration, it will only be applied to the final response of the Agent, and not any intermediate steps. 

## Next Steps

- Configure other [Prompt Settings](/guides/prompt-manager/configuration)
- Learn about using [Tools](/guides/prompt-manager/tools) for more complex interactions.
- Test your JSON-output prompts in the [Playground](/guides/prompt-manager/playground)
